package com.splic.framework.base;

import com.splic.common.core.domain.ApiResult;
import com.splic.common.core.domain.entity.SysUser;
import com.splic.common.enums.ErrorEnum;
import com.splic.common.utils.DateUtils;
import com.splic.common.utils.StringUtils;
import com.splic.common.utils.http.HttpUtils;
import com.splic.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import javax.servlet.http.HttpServletRequest;
import java.beans.PropertyEditorSupport;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * api层通用数据处理
 *
 * @author ruoyi
 */
public class ApiBaseController {
//    @Autowired
//    private JedisPool jedisPool;
    @Autowired
    private ISysUserService userService;
    /**
     * 将前台传递过来的日期格式的字符串，自动转化为Date类型
     */
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        // Date 类型转换
        binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
            @Override
            public void setAsText(String text) {
                setValue(DateUtils.parseDate(text));
            }
        });
    }


    /**
     * 返回成功
     */
    public ApiResult success() {
        return ApiResult.success();
    }

    /**
     * 返回失败消息
     */
    public ApiResult error() {
        return ApiResult.error();
    }

    /**
     * 返回成功消息
     */
    public ApiResult success(String message) {
        return ApiResult.success(message);
    }

    /**
     * 返回错误码消息
     */
    public ApiResult success(ErrorEnum errorEnum) {
        return ApiResult.success(errorEnum);
    }

    /**
     * 返回成功消息
     */
    public ApiResult success(Map<String, Object> map) {
        return ApiResult.success(map);
    }


    /**
     * 返回失败消息
     */
    public ApiResult error(String message) {
        return ApiResult.error(message);
    }

    /**
     * 返回错误码消息
     */
    public ApiResult error(int code, String message) {
        return ApiResult.error(code, message);
    }

    /**
     * 返回错误码消息
     */
    public ApiResult error(ErrorEnum errorEnum) {
        return ApiResult.error(errorEnum);
    }

    public SysUser getSysUser(HttpServletRequest request){
        /*Map<String, String> temp = HttpUtils.getParameterMap(request);
        if (!StringUtils.inspectBy(temp, "token")) {
            return null;
        }
        Jedis jedis = jedisPool.getResource();
        if (!jedis.exists("KEY_LIKE_CUST_LOGIN_TOKEN:" + temp.get("token"))) {
            jedis.close();
            return null;
        }
        //SysUser user = userService.selectUserByLoginName(jedis.get("KEY_LIKE_CUST_LOGIN_TOKEN:" + temp.get("token")));
        SysUser sysUser = new SysUser();
        sysUser.setDouyingId(jedis.get("KEY_LIKE_CUST_LOGIN_TOKEN:" + temp.get("token")));
        List<SysUser> userList = userService.selectUserList(sysUser);
        jedis.close();
        if(userList!=null && userList.size()>0){
            return userList.get(0);
        }*/
        return null;
    }

}
